package com.platform.service.impl;

import com.platform.dao.OrderGoodsDao;
import com.platform.dao.ReturnDao;
import com.platform.dto.OrderExcelDTO;
import com.platform.dto.OrderGoodsReturnDTO;
import com.platform.entity.*;
import com.platform.service.*;
import com.platform.utils.DateUtils;
import com.platform.utils.R;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("returnService")
public class ReturnServiceImpl implements ReturnService {
	@Autowired
	private ReturnDao returnDao;

	@Autowired
	private OrderService orderService;

	@Autowired
	private OrderGoodsService orderGoodsService;
	@Autowired
	private UserFreezeAccountService userFreezeAccountService;

	@Autowired
	private UserService userService;

	@Autowired
	private UserTransactionRecordService userTransactionRecordService;

	@Autowired
	private UserCouponService userCouponService;


	@Override
	public List<ReturnEntity> queryList(Map<String, Object> map) {
		return returnDao.queryList(map);
	}

	@Override
	public OrderGoodsReturnDTO queryDetail(Long id) {
		return returnDao.queryDetail(id);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return returnDao.queryTotal(map);
	}

	@Override
	public ReturnEntity queryObject(Long id) {
		return returnDao.queryObject(id);
	}

	@Override
	@Transactional
	public R saveAudit(OrderGoodsReturnDTO orderGoodsReturnDTO) {
		ReturnEntity dbValue = queryObject(orderGoodsReturnDTO.getId().longValue());
		if (!dbValue.getAuditStatus().equals(ReturnEntity.AUDIT_STATUS_INIT)){
			return R.error("当前订单已审核，无法再次审核");
		}

		OrderEntity orderEntity = orderService.queryObject(dbValue.getOrderId().intValue());

		OrderGoodsEntity orderGoodsEntity = orderGoodsService.queryObject(dbValue.getOrderGoodsId().intValue());

		//退还折扣返点
		if (orderGoodsReturnDTO.getReturnAuditStatus().equals(ReturnEntity.AUDIT_STATUS_PASS)){
			//审核通过
			if (!orderEntity.getUserId().equals(orderEntity.getDiscountUserId())){
				//当前购买人与目标返点人不是同一个人
				if (orderEntity.getDiscountUserId() != null && orderGoodsEntity.getActualDiscountPrice().compareTo(BigDecimal.ZERO) > 0){
					//存在折扣返点，需扣除
					//目标客户冻结表找到当前记录，更新冻结信息

					//增加一条扣除冻结
					UserFreezeAccountEntity oldValue = userFreezeAccountService.getByUserIdAndOrderGoodsId(orderEntity.getDiscountUserId(), dbValue.getOrderGoodsId());
					if (oldValue == null){
						return R.error("扣除当前订单购买人员上级冻结金额失败！");
					}

					Date freezeEnd;
					if (oldValue.getDealStatus().equals(UserFreezeAccountEntity.STATUS_DEAL)){
						freezeEnd = DateUtils.addDate(new Date(), 1);
					} else {
						freezeEnd = oldValue.getFreezeEnd();
					}
					UserFreezeAccountEntity userFreezeAccountEntity = new UserFreezeAccountEntity();
					userFreezeAccountEntity.setUserId(orderEntity.getDiscountUserId());
					userFreezeAccountEntity.setAccount(BigDecimal.ZERO.subtract(orderGoodsEntity.getActualDiscountPrice().multiply(new BigDecimal(orderGoodsReturnDTO.getReturnNumber()))));
					userFreezeAccountEntity.setFreezeEnd(freezeEnd);
					userFreezeAccountEntity.setOrderGoodsId(orderGoodsEntity.getId().longValue());
					userFreezeAccountService.save(userFreezeAccountEntity);

					//流水记录表
					UserTransactionRecordEntity userTransactionRecordEntity = new UserTransactionRecordEntity();
					userTransactionRecordEntity.setMoney(BigDecimal.ZERO.subtract(orderEntity.getActualDiscountPrice()));
					userTransactionRecordEntity.setUserId(orderEntity.getDiscountUserId());
					userTransactionRecordEntity.setTransactionUserId(Long.valueOf(orderEntity.getUserId()));
					userTransactionRecordEntity.setType(UserTransactionRecordEntity.TRANSACTION_TYPE_CANCEL_REBATES);
					userTransactionRecordEntity.setRemark("扣除折扣费返点" + orderGoodsEntity.getActualDiscountPrice().multiply(new BigDecimal(orderGoodsReturnDTO.getReturnNumber())).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "元");
					userTransactionRecordEntity.setOrderId(orderEntity.getId().longValue());
					userTransactionRecordEntity.setOrderGoodsId(dbValue.getOrderGoodsId().longValue());
					userTransactionRecordService.save(userTransactionRecordEntity);
				}
			}

		}

		//取消优惠券
		if (orderEntity.getCouponId() != null){
			UserCouponEntity userCouponEntity = new UserCouponEntity();
			userCouponEntity.setId(orderEntity.getCouponId());
			userCouponEntity.setCouponStatus(UserCouponEntity.STATUS_NORMAL);
			userCouponService.update(userCouponEntity);

			//订单表去掉优惠券信息
			orderService.cancelCoupon(orderEntity.getId());
		}


		orderGoodsService.updateReturn(orderGoodsEntity.getId());

		//401 没有发货，退款　402 已收货，退款退货
		if (orderGoodsReturnDTO.getReturnType().equals(ReturnEntity.TYPE_RG)){
			//退货
			orderEntity.setOrderStatus(402);
		} else {
			//退款
			orderEntity.setOrderStatus(402);
		}
		orderService.update(orderEntity);

		returnDao.saveAudit(orderGoodsReturnDTO);
		return R.ok();
	}

	@Override
	public R closeReturn(ReturnEntity returnEntity) {
		ReturnEntity dbValue = returnDao.queryObject(returnEntity.getId());
		if (dbValue.getAuditStatus().equals(ReturnEntity.AUDIT_STATUS_INIT)){
			return R.error("当前订单没有审核，无法关闭");
		}

		if (dbValue.getStatus().equals(ReturnEntity.STATUS_CLOSE)){
			return R.error("当前订单以关闭，无法再次关闭");
		}
		returnEntity.setStatus(returnEntity.STATUS_CLOSE);
		returnDao.closeReturn(returnEntity);
		return R.ok();
	}
}
